#################################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#################################################################
# Figure 5: Military Intervention: Individual-level heterogeneity
#################################################################

# define which packages are needed 
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

# load data
load("EUP_Costs_ReplicationData.RData")

## Knowledge about the EU: Heterogeneous treatment effect
# Interaction Model: Experiment 1
knowledge_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * quiz + costExperiment1_Treat2 * quiz + QCOUNTRY, data = .)

# Military Intervention: Casualties x Political Knowledge
plot_knowledge_Ex1_1 <- plot_model(knowledge_ex1, type = "pred", terms = c("quiz", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Political Knowledge about the EU", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))

# Military Intervention: Budgetary Costs x Political Knowledge
plot_knowledge_Ex1_2 <- plot_model(knowledge_ex1, type = "pred", terms = c("quiz", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Political Knowledge about the EU", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))



## Attitudes EU Heterogeneous treatment effect
# Interaction Model: Experiment 1
EUatt_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * attitudes_EU + costExperiment1_Treat2 * attitudes_EU + QCOUNTRY, data = .)


# Military Intervention: Casualties x Attitudes towards the EU
plot_EUatt_Ex1_1 <- plot_model(EUatt_ex1, type = "pred", terms = c("attitudes_EU", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Attitudes towards the EU", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 


# Military Intervention: Budgetary Costs x Attitudes towards the EU
plot_EUatt_Ex1_2 <- plot_model(EUatt_ex1, type = "pred", terms = c("attitudes_EU", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Attitudes towards the EU", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 


## Militant internationalism heterogenous treatment effect
# Experiment 1: Interaction Model
MI_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * militarism + costExperiment1_Treat2 * militarism + QCOUNTRY, data = .)



# Military Intervention: Casualties x Militant Internationalism
plot_MI_Ex1_1 <- plot_model(MI_ex1, type = "pred", terms = c("militarism", "costExperiment1_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Militant Internationalism", title = "Support for Military Intervention", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))


# Military Intervention: Budgetary Costs x Militant Internationalism
plot_MI_Ex1_2 <- plot_model(MI_ex1, type = "pred", terms = c("militarism", "costExperiment1_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Militant Internationalism", title = "Support for Military Intervention", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))


# add whitespace to arrange plots
whitespace <- grob("")

# define function to arrange plots
grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
  
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)
  
  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  
  grid.newpage()
  grid.draw(combined)
  
  # return gtable invisibly
  invisible(combined)
  
}

### arrange plots
# Plots: Military Intervention, Non-Budgetary Costs
plot_military_operation_1 <- grid_arrange_shared_legend(plot_knowledge_Ex1_1,
                                                        plot_EUatt_Ex1_1,
                                                        plot_MI_Ex1_1,ncol = 3)
# Plots: Military Intervention, Budgetary Costs
plot_military_operation_2 <- grid_arrange_shared_legend(plot_knowledge_Ex1_2,
                                                        plot_EUatt_Ex1_2,
                                                        plot_MI_Ex1_2,ncol = 3)

# Combine Non-Budgetary and Budgetary Costs Plot into one Figure
military_operation <- grid.arrange(plot_military_operation_1,
                                   whitespace,
                                   plot_military_operation_2,
                                   heights=c(20,1.5,20))

# save Figure
ggsave("plots/het_treatment_military.png", military_operation,
       device = "jpeg", width = 4600 , height = 3000, units = "px")

